//
//  DetaileController.m
//  XKBaseDemo
//
//  Created by xukai on 2017/10/20.
//  Copyright © 2017年 xukai. All rights reserved.
//

#import "DetaileController.h"

@interface DetaileController ()<UIScrollViewDelegate, CAAnimationDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong)  CAAnimationGroup * groupAni;

@end

@implementation DetaileController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self setupUI];
}

-(void) setupUI
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.view.height)];
    _scrollView.delegate = self;
    _scrollView.backgroundColor = ColorFromRGB(0xeeeeee);
    
    [self.view addSubview:_scrollView];
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 350)];
    _imgView.image = [UIImage imageNamed:self.model.icon];
//    [_scrollView addSubview:_imgView];

    [self imgAnimation];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    closeBtn.top = 20;
    closeBtn.right = ScreenW - 20;
    [closeBtn setTitle:@"Close" forState:(UIControlStateNormal)];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:(UIControlEventTouchUpInside)];
    
    _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, _imgView.bottom + 40, ScreenW - 40, 600)];
    [_scrollView addSubview:_titleLab];
    _titleLab.numberOfLines = 0;
    _titleLab.font = [UIFont systemFontOfSize:18];
    _titleLab.textColor = [UIColor blackColor];
    _titleLab.text = @"四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒四渡赤水第三第三方的还是房价肯定康师傅咖啡看到啦回复分解都是废话的可是翻江倒海附近的复合地基开始发货快点撒";
    
    _scrollView.contentSize = CGSizeMake(0, _titleLab.bottom);
    
    [_scrollView addSubview:_imgView];
    [self.view addSubview:closeBtn];


}


-(void) imgAnimation
{
    ViewRadius(_imgView, 10);
    CABasicAnimation *baseAnimate = [CABasicAnimation animationWithKeyPath:@"position.y"];
    baseAnimate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//
    baseAnimate.fromValue = @(self.rect.origin.y + _imgView.height * 0.5); // 开始时的倍率
    baseAnimate.toValue = @(_imgView.centerY); // 结束时的倍率
    
    
    CGFloat s1 = (ScreenW - 20.0 *2) / ScreenW;
    CABasicAnimation *baseAnimate1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    baseAnimate1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//
    baseAnimate1.fromValue = [NSNumber numberWithFloat:s1];
    baseAnimate1.toValue = [NSNumber numberWithFloat:1];
    
    CGFloat s2 = 380.0/350.0;
    CABasicAnimation *baseAnimate2 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    baseAnimate2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//
    baseAnimate2.fromValue = [NSNumber numberWithFloat:s2];
    baseAnimate2.toValue = [NSNumber numberWithFloat:1];
    baseAnimate2.delegate = self;
    
    _groupAni = [CAAnimationGroup animation];
    _groupAni.animations = @[baseAnimate, baseAnimate1, baseAnimate2];
    _groupAni.duration = 0.5;
    _groupAni.removedOnCompletion = NO;
    _groupAni.fillMode = kCAFillModeForwards;
    [_imgView.layer addAnimation:_groupAni forKey:@"groupAni"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ViewRadius(_imgView, 0.5);
    });
    
}




-(void) close
{
    if([self.delegate respondsToSelector:@selector(closeWithRect:)])
    {
        // 计算图片位置
        CGRect rect = [_scrollView convertRect:self.imgView.frame toView:self.view];
        [self.delegate closeWithRect:rect];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
